package cn.com.yusys.yusp.admin.quartz;

import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Calendar;
import java.util.Date;

@Configuration
public class QuartzSendMessageConfig {

    @Value("${quartz.sendMessageCron}")
    private String sendMessageCron;

    @Bean
    public JobDetail doSendMessageJobDetail() {
        return JobBuilder.newJob(SendMessageJob.class)
                .withIdentity("sendMessage")//给改JobDetail一个id
                //每个JobDetail内都有一个Map，包含了关联到这个Job的数据，在Job类中可以通过context获取
//                .usingJobData(jobDataMap)//关联键值对
                .storeDurably()//即时没有Trigger关联是，也不需要删除该JobDetail
                .build();
    }

    @Bean
    public Trigger doSendMessageJobTrigger() {

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 47);
        calendar.set(Calendar.SECOND, 30);
        Date date = calendar.getTime();

//        Environment env = SpringContextUtil.applicationContext.getEnvironment();
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(sendMessageCron);
//        CronScheduleBuilder endDate = CronScheduleBuilder.cronSchedule(endDateCron);
//        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 48 9 * * ?");
//        System.out.println("HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH");
//        System.out.println(umSyncUserCron);
        return TriggerBuilder.newTrigger().forJob(doSendMessageJobDetail())//关联上述的JobDetail
                .withIdentity("sendMessageTrigger")//给Trigger一个id
                .startNow()
                .withSchedule(cronScheduleBuilder)
//                .modifiedByCalendar("annualday")
//                .endAt(date)
                .build();
    }
}
